package main

import (
	"io/ioutil"
	"net/http"
	"net/http/httputil"
	"strings"
)

func main() {
	proxy := httputil.ReverseProxy{Director: func(request *http.Request) {
		if request.ContentLength > 0 {
			bd := request.Body
			body, e := ioutil.ReadAll(bd)
			if e != nil {
				println("read error = " + e.Error())
			}
			println(string(body))
			request.Body = ioutil.NopCloser(strings.NewReader(string(body)))
		} else {
			println("there is no body")
		}
		err := request.ParseForm()
		if err != nil {
			println("ERROR:" + err.Error())
		}
		//简单的通过区分前缀的方式来分发
		switch {
		case strings.HasPrefix(request.RequestURI, "/review")://评审服务
			request.URL.Host = "192.168.3.99:10102"
			break
		default:
			request.URL.Host = "192.168.3.99:10101"
		}
		request.URL.Scheme = "http"
	}}
	println("server config done")
	err := http.ListenAndServe(":9999", &proxy)
	if err != nil {
		println("ERROR:" + err.Error())
	}
}
